<?php

class NhapController {

    public function invoke() {
        $dir = dirname(__FILE__);

        $action = '';
        if (isset($_POST['hdAction'])) {
            $action = $_POST['hdAction'];
        }

        // tác vụ
        $task = '';
        if (isset($_GET['task'])) {
            $task = $_GET['task'];
        }
   
        if ($action == 'remove') {
            // mảng danh sách giá trị id
            $values = $_POST["id"];
 
            $result = $this->deleteChi($values);
           
            
            if ($result == OK) {
//                Utility::messageBox('Xóa thành công');
                 echo'OK';
            
            }
             exit();
        } else if ($action == 'add') {
            $hinhthucthanhtoan=$_POST['hinhthucthanhtoan'];
            $BEP_ID=$_POST['BEP_ID'];
            $NHAP_NGAY_NHAP=$_POST['NHAP_NGAY_NHAP'];
            $LY_DO_NHAP=$_POST['LY_DO_NHAP'];
            $NHAP_TONG_TIEN_PHAI_TRA=$_POST['NHAP_TONG_TIEN_PHAI_TRA'];
            $DON_GIA=$_POST['DON_GIA'];
            $SOLUONG=$_POST['SOLUONG'];
            $THANHTIEN=$_POST['THANHTIEN'];
            $ID_HH=$_POST['ID_HH'];
            $result=null;
            if($hinhthucthanhtoan=='1'){
            $result = $this->addNhap($BEP_ID, $NHAP_NGAY_NHAP,$LY_DO_NHAP,$NHAP_TONG_TIEN_PHAI_TRA,$DON_GIA,$SOLUONG,$THANHTIEN,$ID_HH);
            }
            if ($result == OK) {
                if ($action == 'add') {
                    echo 'OK';
                }
            }
             exit();
             
            
        } else if ($action == 'edit' ) {

            $NHAP_ID=$_POST['NHAP_ID'];
            $BEP_ID=$_POST['BEP_ID'];
            $NHAP_NGAY_NHAP=$_POST['NHAP_NGAY_NHAP'];
            $LY_DO_NHAP=$_POST['LY_DO_NHAP'];
            $NHAP_TONG_TIEN_PHAI_TRA=$_POST['NHAP_TONG_TIEN_PHAI_TRA'];
            $DON_GIA=$_POST['DON_GIA'];
            $SOLUONG=$_POST['SOLUONG'];
            $THANHTIEN=$_POST['THANHTIEN'];
            $ID_HH=$_POST['ID_HH'];
            
            $result1 = $this->deleteChiTietNhap($NHAP_ID);
             
            $result2 = $this->deleteChi_chitiet($NHAP_ID);
            $result = $this->updateNhap($NHAP_ID,$BEP_ID, $NHAP_NGAY_NHAP,$LY_DO_NHAP,$NHAP_TONG_TIEN_PHAI_TRA,$DON_GIA,$SOLUONG,$THANHTIEN,$ID_HH);
            if ($result == OK) {
                if ($action == 'edit') {
                    echo 'OK';
                }
            }
             exit();
            
            

            
        }


        if ($task == 'add') {
            $xtemplate = new XTemplate('./components' . DS . 'com_logic_nhap' . DS . 'views' . DS . 'nhap_add.php');
            
            $beps=$this->getAllBep();
            
              if ($beps != null) {
                 
                foreach ($beps as $bep) {

                     
                     $xtemplate->assign('BEP_ID', $bep->getBEP_ID());
                    $xtemplate->assign('BEP_TEN_BEP', $bep->getBEP_TEN_BEP());
                    $xtemplate->parse('main.bep');
                    
                }
            }
           $hanghoas = $this->getAllHangHoa();


            if ($hanghoas != null) {
                 
                foreach ($hanghoas as $hanghoa) {

                    $xtemplate->assign('H_ID', $hanghoa->getID());
                    $xtemplate->assign('H_TEN', $hanghoa->getTEN_HANG_HOA());
                    $xtemplate->assign('H_GIA_NHAP',$hanghoa->getDON_GIA_NHAP() );
                    $xtemplate->assign('H_GIA_BAN', $hanghoa->getDON_GIA_BAN());
                    $xtemplate->assign('H_GIA_BAN_SI', $hanghoa->getDON_GIA_SI());
                    $xtemplate->assign('H_GIA_BAN_LE', $hanghoa->getDON_GIA_LE());
                   
                    
                    
                    
                    $xtemplate->parse('main.hanghoa');
                    
                }
            }
            
            $hanghoas_gia_soluongs = $this->getAllHangHoa();


            if ($hanghoas_gia_soluongs != null) {
                 
                foreach ($hanghoas_gia_soluongs as $hanghoa) {

                    $xtemplate->assign('H_ID', $hanghoa->getID());
                    $xtemplate->assign('H_TEN', $hanghoa->getTEN_HANG_HOA());
                    $xtemplate->assign('H_GIA_NHAP',$hanghoa->getDON_GIA_NHAP() );
                    $xtemplate->assign('H_GIA_BAN', $hanghoa->getDON_GIA_BAN());
                    $xtemplate->assign('H_GIA_BAN_SI', $hanghoa->getDON_GIA_SI());
                    $xtemplate->assign('H_GIA_BAN_LE', $hanghoa->getDON_GIA_LE());
                    
                    
                    
                    
                    $xtemplate->parse('main.hanghoas_gia_soluong');
                    
                }
            }

            
             $xtemplate->assign('DATE', Utility::getCurrentDate());
            $xtemplate->parse('main');
            $xtemplate->out('main');
        }
        // nếu task là edit thì include trang thêm vào
        else if ($task == 'edit') {
            $id = $_GET['idnhap'];
            $xtemplate = new XTemplate('./components' . DS . 'com_logic_nhap' . DS . 'views' . DS . 'nhap_edit.php');
            $chi = $this->getNhapById($id);
//            Utility::messageBox($id);

            if ($chi != null) {
                
 
                    $xtemplate->assign('NHAP_ID', $chi->getNHAP_ID());
                    $xtemplate->assign('BEP_ID', $chi->getBEP_ID());
                    $xtemplate->assign('NHAP_NGAY_NHAP', $chi->getNHAP_NGAY_NHAP());
                    $xtemplate->assign('NHAP_LY_DO_NHAP', $chi->getNHAP_LY_DO_NHAP());
                    $xtemplate->assign('NHAP_TONG_TIEN_PHAI_TRA', $chi->getNHAP_TONG_TIEN_PHAI_TRA());
                     $beps=$this->getAllBep();
             $option_bep='';
              if ($beps != null) {
                 
                foreach ($beps as $bep) {

                     
                      if($chi->getBEP_ID()==$bep->getBEP_ID()){
                          $option_bep .="<option selected=selected value='".$bep->getBEP_ID()."'>".$bep->getBEP_TEN_BEP()."</option>";
                          }else{
                             $option_bep .="<option  value='".$bep->getBEP_ID()."'>".$bep->getBEP_TEN_BEP()."</option>";
                            
                          }
                    
                }
            }
                    
                    
                    $xtemplate->assign('BEP', $option_bep);
                    
                    
                    
                    
                    $hanghoas = $this->getAllHangHoa();


            if ($hanghoas != null) {
                 
                foreach ($hanghoas as $hanghoa) {
                    
                    $chitietnhap=$this->getChiTietNhapById($chi->getNHAP_ID(),$hanghoa->getID());
                    if($chitietnhap!=null){
                        $xtemplate->assign('H_ID', $hanghoa->getID());
                    $xtemplate->assign('H_TEN', $hanghoa->getTEN_HANG_HOA());
                    $xtemplate->assign('H_GIA_NHAP',$chitietnhap->getCT_DON_GIA() );
                    $xtemplate->assign('SO_LUONG',$chitietnhap->getCT_SO_LUONG());
                    }else{
                    $xtemplate->assign('H_ID', $hanghoa->getID());
                    $xtemplate->assign('H_TEN', $hanghoa->getTEN_HANG_HOA());
                    $xtemplate->assign('H_GIA_NHAP',$hanghoa->getDON_GIA_NHAP() );
                    $xtemplate->assign('SO_LUONG',0);
                    }
                    
                    
                    $xtemplate->parse('main.nhap.hanghoa');
                    
                }
            }
                    
                  $hanghoas_gia_soluongs = $this->getAllHangHoa();


            if ($hanghoas_gia_soluongs != null) {
                 
                foreach ($hanghoas_gia_soluongs as $hanghoa) {

                    $xtemplate->assign('H_ID', $hanghoa->getID());
                    $xtemplate->assign('H_TEN', $hanghoa->getTEN_HANG_HOA());
                    $xtemplate->assign('H_GIA_NHAP',$hanghoa->getDON_GIA_NHAP() );
                    $xtemplate->assign('H_GIA_BAN', $hanghoa->getDON_GIA_BAN());
                    $xtemplate->assign('H_GIA_BAN_SI', $hanghoa->getDON_GIA_SI());
                    $xtemplate->assign('H_GIA_BAN_LE', $hanghoa->getDON_GIA_LE());
                    
                    
                    
                    
                    $xtemplate->parse('main.nhap.hanghoas_gia_soluong');
                    
                }
            }  
                    
                    
                    $xtemplate->parse('main.nhap');
                   
                
            }




            $xtemplate->parse('main');
            $xtemplate->out('main');
            
            
        } 
        else {
            $viewFile = $dir . '/views/nhap.php';
            $xtemplate = new XTemplate($viewFile);
            $chis = $this->getAllChi();


            if ($chis != null) {
                foreach ($chis as $chi) {

                    $xtemplate->assign('NHAP_ID', $chi->getNHAP_ID());
                     $xtemplate->assign('NHAP_NGAY_NHAP',Utility::formatDateTimeToView($chi->getNHAP_NGAY_NHAP()) );
                    $xtemplate->assign('BEP', $this->getBepById($chi->getBEP_ID())->getBEP_TEN_BEP());
                    $xtemplate->assign('NHAP_TONG_TIEN_PHAI_TRA',Utility::formatToCurrencyString($chi->getNHAP_TONG_TIEN_PHAI_TRA()) );
                     $xtemplate->assign('NHAP_TONG_TIEN_DA_TRA',Utility::formatToCurrencyString($chi->getNHAP_TONG_TIEN_DA_TRA()) );
                    
                    
                    
                    
                    $xtemplate->parse('main.chi');
                   
                }
            }




            $xtemplate->parse('main');
            $xtemplate->out('main');
        }
        
        
        
        
        
        
        
        
    }

 
     private function getHoaHongById($id,$truong) {
        $db = new Database();
        try {
            $hoahong = new Hoa_hong();
            $result = $hoahong->getByID($id,$truong,$db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }
    
    private function getArrayHoaHongById($truong) {
        $db = new Database();
        try {
            $hoahong = new Hoa_hong();
            $result = $hoahong->getArrayByID($truong,$db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }
    
    
   
     private function  getAllLoaiHoaHong() {
        $db = new Database();
        try {
            $loaihoahong = new LoaiHoaHong();

            $result = $loaihoahong->getList($db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }
    
     
    
    
    
   
    
        private function updateNhap($NHAP_ID,$BEP_ID, $NHAP_NGAY_NHAP,$LY_DO_NHAP,$NHAP_TONG_TIEN_PHAI_TRA,$DON_GIA,$SOLUONG,$THANHTIEN,$ID_HH){
        
         $db = new Database();
        try {            
           
           
                $nhap = new Nhap();
                $nhap->setNHAP_ID($NHAP_ID);
                 $nhap->setBEP_ID($BEP_ID);
                 $date = new DateTime($NHAP_NGAY_NHAP);
                 $nhap->setNHAP_NGAY_NHAP($date->format("Y-m-d"));
                 $nhap->setNHAP_LY_DO_NHAP($LY_DO_NHAP);
                 $nhap->setNHAP_TONG_TIEN_PHAI_TRA($NHAP_TONG_TIEN_PHAI_TRA);
                 $nhap->setNHAP_TONG_TIEN_DA_TRA($NHAP_TONG_TIEN_PHAI_TRA);
                 $nhap->setIS_ACTIVE(1);
                $nhap->update($db);
               
               
                
                $chi=new Chi();
                $chi->setCHI_NGAY_CHI($date->format("Y-m-d"));
                $chi->setCHI_SO_TIEN($NHAP_TONG_TIEN_PHAI_TRA);
                $chi->setCHI_PHAN_LOAI("nhaphang");
                $chi->setIS_ACTIVE(1);
                $chi->insert($db);
                
                $chinhaphang=new Chi_nhap_hang();
                $chinhaphang->setNHAP_ID($nhap->getNHAP_ID());
                $chinhaphang->setCHI_ID($chi->getCHI_ID());
                $chinhaphang->setSO_TIEN($NHAP_TONG_TIEN_PHAI_TRA);
                $chinhaphang->insert($db);
                 
                
                $DON_GIA_ = explode(',', $DON_GIA);
                $SOLUONG_= explode(',', $SOLUONG);
                $THANHTIEN_  = explode(',', $THANHTIEN);     
                $ID_HH_= explode(',', $ID_HH);  
                for ($i = 0; $i < sizeof($ID_HH_); $i++) {
                    
                $chitietnhap=new Chi_tiet_nhap();
                $chitietnhap->setNHAP_ID($nhap->getNHAP_ID());
                $chitietnhap->setHH_ID($ID_HH_[$i]);
                $chitietnhap->setCT_DON_GIA($DON_GIA_[$i]);
                $chitietnhap->setCT_SO_LUONG($SOLUONG_[$i]);
                $chitietnhap->setCT_THANH_TIEN($THANHTIEN_[$i]);
                $chitietnhap->setIS_ACTIVE(1);
                $chitietnhap->insert($db);
            }
                
                
               
            $db->submitChanges();
            return OK;
        }
        catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
        
    }
    
    

    
    
    
   //----------------------đang sử dụng 
    
    
    private function deleteChiTietNhap($values) {
        $db = new Database();
        try {            
           
                $chitietnhap = new Chi_tiet_nhap();
                $chitietnhap->setNHAP_ID($values);
                $chitietnhap->delete($db);
            
            $db->submitChanges();
            return OK;
        }
        catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }
    
    
    private function deleteChi_chitiet($values) {
        $db = new Database();
        try {            
           
                $chi_nhap_hang = new Chi_nhap_hang();
                $chi_nhap_hang->setNHAP_ID($values);
                $chi_nhap_hang->delete($db);
            
            $db->submitChanges();
            return OK;
        }
        catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }
    
      private function getChiTietNhapById($chi,$hanghoa) {
        $db = new Database();
        try {
            $chitietnhap = new Chi_tiet_nhap();
            $result = $chitietnhap->getByID($chi,$hanghoa,$db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }
    
    
    
       private function getAllChi() {
        $db = new Database();
        try {
            $nhap= new Nhap();

            $result = $nhap->getList($db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }
  private function getNhapById($id) {
        $db = new Database();
        try {
            $nhap = new Nhap();
            $nhap->setNHAP_ID($id);
            $result = $nhap->getByID($db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }
    
    
      private function getBepById($id) {
        $db = new Database();
        try {
            $bep = new Bep();
            $bep->setBEP_ID($id);
            $result = $bep->getByID($db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }
    
    
     private function addNhap($BEP_ID, $NHAP_NGAY_NHAP,$LY_DO_NHAP,$NHAP_TONG_TIEN_PHAI_TRA,$DON_GIA,$SOLUONG,$THANHTIEN,$ID_HH){
        
         $db = new Database();
        try {            
           
            
                $nhap = new Nhap();
                $maxTuan=$nhap->getMaxID_($db);
                if($maxTuan->getNHAP_ID() ==''){
                    
                    $nhap->setTUAN_ID(1);   
                     $nhap->setTHANG_ID(1);
                }else{
                   $nhap_max=new Nhap();
                   $nhap_max->setNHAP_ID($maxTuan->getNHAP_ID());
                   $nhap_maxs=$nhap_max->getByID($db);
//                    echo $nhap_maxs->getNHAP_NGAY_NHAP();exit();
                    if(Utility::getWeekDates($NHAP_NGAY_NHAP,$nhap_maxs->getNHAP_NGAY_NHAP())){
                        
                         $nhap->setTUAN_ID($nhap_maxs->getTUAN_ID());   
                         $nhap->setTHANG_ID($nhap_maxs->getTHANG_ID());
                          
                    }else{
                       
                        if($nhap_maxs->getTUAN_ID()<4){//echo $nhap_maxs->getTUAN_ID();exit();
                             $nhap->setTUAN_ID($nhap_maxs->getTUAN_ID()+1);   
                             $nhap->setTHANG_ID($nhap_maxs->getTHANG_ID());
                              
                        }else{
                             $nhap->setTUAN_ID(1);   
                             $nhap->setTHANG_ID($nhap_maxs->getTHANG_ID()+1);
                        }
                    }
                    
                }
                
                
                if($nhap->getMaxID($db)->getNHAP_SO_PHIEU_INT()!=''){
                    $MaxId=($nhap->getMaxID($db)->getNHAP_SO_PHIEU_INT())+1;
                    switch (strlen($MaxId)) {
                    case 1:
                        $nhap->setNHAP_SO_PHIEU_NHAP("PN00000".$MaxId.$nhap->getMaxID($db)->getNHAP_SO_PHIEU_NHAP());
                        break;
                   case 2:
                         $nhap->setNHAP_SO_PHIEU_NHAP("PN0000".$MaxId.$nhap->getMaxID($db)->getNHAP_SO_PHIEU_NHAP());
                        break;
                    case 3:
                         $nhap->setNHAP_SO_PHIEU_NHAP("PN000".$MaxId.$nhap->getMaxID($db)->getNHAP_SO_PHIEU_NHAP());
                        break;
                    case 4:
                         $nhap->setNHAP_SO_PHIEU_NHAP("PN00".$MaxId.$nhap->getMaxID($db)->getNHAP_SO_PHIEU_NHAP());
                        break;
                    case 5:
                         $nhap->setNHAP_SO_PHIEU_NHAP("PN0".$MaxId.$nhap->getMaxID($db)->getNHAP_SO_PHIEU_NHAP());
                        break;
                    case 6:
                         $nhap->setNHAP_SO_PHIEU_NHAP("PN".$MaxId.$nhap->getMaxID($db)->getNHAP_SO_PHIEU_NHAP());
                        break;

                    default:
                        break;
                }
                
                 $nhap->setNHAP_SO_PHIEU_INT($MaxId);
                }else{
                    $nhap->setNHAP_SO_PHIEU_NHAP("PN000001".date("Y"));
                     $nhap->setNHAP_SO_PHIEU_INT(1);
                }
                
               
               
                $nhap->setBEP_ID($BEP_ID);
                
                 $date = new DateTime($NHAP_NGAY_NHAP);
                 $nhap->setNHAP_NGAY_NHAP($date->format("Y-m-d"));
                 $nhap->setNHAP_LY_DO_NHAP($LY_DO_NHAP);
                 $nhap->setNHAP_TONG_TIEN_PHAI_TRA($NHAP_TONG_TIEN_PHAI_TRA);
                 $nhap->setNHAP_TONG_TIEN_DA_TRA($NHAP_TONG_TIEN_PHAI_TRA);
                 $nhap->setIS_ACTIVE(1); 
                  $nhap->setNHAP_ID(null);
                $nhap->insert($db);
              
                $chi=new Chi();
                $chi->setCHI_NGAY_CHI($date->format("Y-m-d"));
                $chi->setCHI_SO_TIEN($NHAP_TONG_TIEN_PHAI_TRA);
                $chi->setCHI_PHAN_LOAI('nhaphang');
                $chi->setIS_ACTIVE(1);
                $chi->insert($db);
                
                $chinhaphang=new Chi_nhap_hang();
                $chinhaphang->setNHAP_ID($nhap->getNHAP_ID());
                $chinhaphang->setCHI_ID($chi->getCHI_ID());
                $chinhaphang->setSO_TIEN($NHAP_TONG_TIEN_PHAI_TRA);
                $chinhaphang->insert($db);
                
                $DON_GIA_ = explode(',', $DON_GIA);
                $SOLUONG_= explode(',', $SOLUONG);
                $THANHTIEN_  = explode(',', $THANHTIEN);     
                $ID_HH_= explode(',', $ID_HH);  
                for ($i = 0; $i < sizeof($ID_HH_); $i++) {
                    
                $chitietnhap=new Chi_tiet_nhap();
                $chitietnhap->setNHAP_ID($nhap->getNHAP_ID());
                $chitietnhap->setHH_ID($ID_HH_[$i]);
                $chitietnhap->setCT_DON_GIA($DON_GIA_[$i]);
                $chitietnhap->setCT_SO_LUONG($SOLUONG_[$i]);
                $chitietnhap->setCT_THANH_TIEN($THANHTIEN_[$i]);
                $chitietnhap->setIS_ACTIVE(1);
                $chitietnhap->insert($db);
            }
                
               
               
            $db->submitChanges();
            return OK;
        }
        catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
        
    }
    
    
    private function getAllBep() {
        $db = new Database();
        try {
            $bep = new Bep();

            $result = $bep->getList($db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }
    
    private function getAllHangHoa() {
        $db = new Database();
        try {
            $hanghoa = new Hanghoa();

            $result = $hanghoa->getList_($db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }
    
        private function deleteChi($values) {
        $db = new Database();
        try {            
           
                $nhap = new Nhap();
                $nhap->setNHAP_ID($values);
                $nhap->delete($db);
                
                $chitietnhap=new Chi_tiet_nhap();
                 $chitietnhap->setNHAP_ID($values);
                $chitietnhap->delete($db);
                
                $chinhaphang=new Chi_nhap_hang();
                 $chinhaphang->setNHAP_ID($values);
                $chinhaphangs=$chinhaphang->getListByIdNhap($db);
                if($chinhaphangs!=null){
                    
                     foreach ($chinhaphangs as $chinhaphang) {
                        
                         $chi=new Chi();
                         $chi->setCHI_ID($chinhaphang->getCHI_ID());
                         $chi->delete($db);
                    }
                }
                $chinhaphang->delete($db);
                
                
                
            $db->submitChanges();
            return OK;
        }
        catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }
    
    
    
    

    
}
